package com.haiertvbic.tvnow.service;

import java.nio.ByteBuffer;

import com.haiertvbic.tvnow.protocol.ConnectManager;

public class VDService {
	private static int SERIAL_NUMBER = 0;
	public static final int REQUEST_GET_CURRENT_SIGNAL_SOURCE = 1;

	/**
	 * 向CAE询问当前的电视信号源 4表示其他
	 */
	public static void getCurrentSignalSource(final SourceCallback callback) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				boolean status = false;
				String error = "";
				int source = -1;

				SocketClient client = new SocketClient(
						ConnectManager.LOCAL_HOST,
						ConnectManager.SERVICE_HOST_PORT);
				client.connect();
				if (client.getStatus() == SocketClient.SOCKET_STATUS_READY) {
					int dataSize = 20;
					// Send Data
					ByteBuffer sBuffer = ByteBuffer.allocate(dataSize);
					sBuffer.put(SmallEndianConvertor
							.toBytes(ConnectManager.CMD_VD_REQ_CURRENT_SIG_SOURCE));
					sBuffer.put(SmallEndianConvertor.toBytes(getSerialNumber()));
					sBuffer.put(SmallEndianConvertor.toBytes(0));
					sBuffer.put(SmallEndianConvertor.toBytes(4));
					sBuffer.put(SmallEndianConvertor.toBytes(0));

					if (client.sendData(sBuffer.array()) == dataSize) {
						// Receive Data
						byte[] rBuffer = new byte[4];
						int count = 0, command = 0;
						count = client.receiveData(rBuffer); // Command
						if (count == 4) {
							command = SmallEndianConvertor.toInteger(rBuffer);
							if (command == ConnectManager.CMD_VD_REQ_CURRENT_SIG_SOURCE_RE) {
								client.receiveData(rBuffer); // SerialNumber
								client.receiveData(rBuffer); // Retain
								client.receiveData(rBuffer); // Data Length
								client.receiveData(rBuffer); // Return Code
								client.receiveData(rBuffer); // Source
								source = SmallEndianConvertor
										.toInteger(rBuffer);
								client.receiveData(rBuffer); // Retain
								status = true;
							} else
								error = "Invalid command back";
						}
					}
					sBuffer.clear();
				}
				client.disconnect();

				if (!status && error.length() == 0)
					error = SocketClient.getErrorsMessage(client.getErrors());

				callback.returnData(source);
			}
		}).start();
	}

	// 通过全国id向CAE请求dtv的id
	public static int getDTVIdWithWholeCountryId(int tv_mod, short wc_id) {
		// TODO Auto-generated method stub
		// Send Data
		byte[] tv_name = new byte[64];
		// Receive Data
		short r_dtv_cid = 0; // 返回dtv的ID

		SocketClient client = new SocketClient(ConnectManager.LOCAL_HOST,
				ConnectManager.CHANNEL_PORT);
		client.connect();
		if (client.getStatus() == SocketClient.SOCKET_STATUS_READY) {
			int dataSize = 86;
			// Send Data
			ByteBuffer sBuffer = ByteBuffer.allocate(dataSize);
			sBuffer.put(BigEndianConvertor
					.toBytes(ConnectManager.CMD_SMART_TV_ASK_LOCAL_ID_BY_COUNTRY_ID));
			sBuffer.put(BigEndianConvertor.toBytes(getSerialNumber()));
			sBuffer.put(BigEndianConvertor.toBytes(0));
			sBuffer.put(BigEndianConvertor.toBytes(70));
			sBuffer.put(BigEndianConvertor.toBytes(tv_mod));
			sBuffer.put(BigEndianConvertor.toBytes(wc_id));
			sBuffer.put(tv_name);

			if (client.sendData(sBuffer.array()) == dataSize) {
				// Receive Data
				byte[] rBuffer = new byte[4];
				int count = 0, command = 0;
				count = client.receiveData(rBuffer); // Read Command
				if (count == 4) {
					command = BigEndianConvertor.toInteger(rBuffer);
					if (command == ConnectManager.CMD_SMART_TV_ASK_LOCAL_ID_BY_COUNTRY_ID_RE) {
						client.receiveData(rBuffer); // Read Serial
														// Number
						client.receiveData(rBuffer); // Read Retain
						client.receiveData(rBuffer); // Read Data Length
						client.receiveData(rBuffer); // Read Return Code
						client.receiveData(rBuffer);
						rBuffer = new byte[2];
						client.receiveData(rBuffer);
						r_dtv_cid = BigEndianConvertor.toShort(rBuffer);

					}
				}
			}
			sBuffer.clear();
		}
		client.disconnect();
		return r_dtv_cid;

	}

	static synchronized int getSerialNumber() {
		return (SERIAL_NUMBER = ++SERIAL_NUMBER % 65536);
	}

}
